package com.zhuangjie.user.controller;


import com.zhaungjie.base.conduit.R;
import com.zhaungjie.base.utils.PropertiesCopyUtils.PropertiesCopy;
import com.zhuangjie.base.constant.ModuleSecurityNames;
import com.zhuangjie.security.annotations.NoAuthentication;
import com.zhuangjie.security.annotations.PublicAPI;
import com.zhuangjie.security.entity.AuthenticationTokens;
import com.zhuangjie.security.helper.JwtHelper;
import com.zhuangjie.security.provider.UserProvider;
import com.zhuangjie.user.dto.WeixinLoginDTO;
import com.zhuangjie.user.entity.User;
import com.zhuangjie.user.service.UserService;
import com.zhuangjie.user.vo.AssetsStatisticsVO;
import com.zhuangjie.user.vo.UserInfoVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.concurrent.ExecutionException;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author zhuangjie
 * @since 2023-10-15
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private JwtHelper jwtHelper;

    @PostMapping("/loginForWeixin")
    @NoAuthentication
    @ApiOperation("微信登录")
    public R wxLogin(@Valid @RequestBody WeixinLoginDTO wxLoginInput) throws Exception {
        AuthenticationTokens authenticationTokens = userService.wxLogin(wxLoginInput);
        return R.ok().data(authenticationTokens);
    }
    @GetMapping("/getUserInfo")
    @ApiOperation("获取用户信息")
    public R getUserInfo() {
        User user = userService.getById(String.valueOf(UserProvider.currentUserId()));
        UserInfoVO userInfoVO = PropertiesCopy.type(User.class,UserInfoVO.class).produce(user);
        return R.require(userInfoVO);
    }

    @GetMapping("/refreshToken")
    @ApiOperation("使用refresh_token刷新获取新的token信息")
    @NoAuthentication
    public R refreshToken(@NotBlank(message = "需要传入token") @RequestParam("refreshToken") String refreshToken) throws Exception {
        return R.require(userService.refreshToken
                (refreshToken));
    }

    @GetMapping("/openId")
    @ApiOperation("内部接口-获取用户tokenId")
    @PublicAPI(modules = {ModuleSecurityNames.ORDER})
    public R getUserOpenId(@NotNull(message = "必须指定用户id") @RequestParam("userId") Long userId) {
        return R.ok().data(String.class, userService.getUserWXOpenId(userId));
    }

    @ApiOperation("资产统计")
    @GetMapping("/assets-statistics")
    public R assetsStatistics() throws ExecutionException, InterruptedException {
        return R.require(AssetsStatisticsVO.class,userService.assetsStatistics());
    }


}

